package com.yuke.cloud.service.smc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.PublicUtil;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.pmc.dto.ProdCategoryDto;
import com.yuke.cloud.service.smc.dto.*;
import com.yuke.cloud.service.smc.service.ShopGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by wg on 2018/09/10.
*/
@Slf4j
@Api(value = "WEB - ShopGoodsController", tags = "处理ShopGoods相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/mall/shop/goods")
public class MallShopGoodsController extends BaseController {
    @Resource
    private ShopGoodsService shopGoodsService;

    //通过spec_id,longitude,latitude获取商品店铺等详情
    @GetMapping("/app/spec/{specId}")
    @ApiOperation(httpMethod = "GET", value = "根据具体商品规格ID获取所有售卖该商品的店铺以及货物信息")
    public Wrapper<PageInfo<ShopAndGoodsInfoDto>> getShopAndGoodsBySpecId(@ApiParam(name = "specId", value = "specId") @PathVariable Long specId,
                                                               @RequestParam(value = "longitude", defaultValue = "") String longitude,
                                                               @RequestParam(value = "latitude", defaultValue = "") String latitude,
                                                                      @RequestParam(value = "userId", required = false, defaultValue = "") Long userId,
                                                                      @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                      @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        if (PublicUtil.isEmpty(longitude) || PublicUtil.isEmpty(latitude)) {
            return WrapMapper.error("无法定位！");
        }

        PageHelper.startPage(page, size);

        List<ShopAndGoodsInfoDto> shopAndGoodsInfoDtos = shopGoodsService.getShopAndGoodsBySpecId(specId, longitude, latitude, userId);
//        return WrapMapper.ok(shopAndGoodsInfoDtos);
        PageInfo pageInfo = new PageInfo(shopAndGoodsInfoDtos);
        return WrapMapper.ok(pageInfo);
    }

    @PostMapping("/app/search")
    @ApiOperation(httpMethod = "POST", value = "搜索栏搜索商品")
    public Wrapper<PageInfo<ShopAndGoodsInfoDto>> getShopAndGoodsBySearch(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                      @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                                      @ApiParam(name = "searchDto", value = "search搜索名称") @RequestBody SearchDto search) {
        if (search.getLongitude() == null || search.getLatitude() == null) {
            return WrapMapper.error("无法定位！");
        }

        PageHelper.startPage(page, size);

        List<ShopAndGoodsInfoDto> shopAndGoodsInfoDtos = shopGoodsService.getShopAndGoodsBySearch(search.getSearch(),
                search.getLongitude(), search.getLatitude(), search.getUserId());
//        return WrapMapper.ok(shopAndGoodsInfoDtos);
        PageInfo pageInfo = new PageInfo(shopAndGoodsInfoDtos);
        return WrapMapper.ok(pageInfo);
    }

    @GetMapping("/app/shop/{shopId}")
    @ApiOperation(httpMethod = "GET", value = "根据店铺ID获取该店铺所售卖的商品")
    public Wrapper<ShopGoodsInfoDto> getShopGoodsByShopId(@ApiParam(name = "shopId", value = "shopId") @PathVariable Long shopId) {
        //TODO:后期不一次返回所有数据
        ShopGoodsInfoDto shopGoodsInfoDto = shopGoodsService.getShopGoodsInfoByShopId(shopId);
        return WrapMapper.ok(shopGoodsInfoDto);
    }

    @GetMapping("/app/shop/category/isAlive/{shopId}")
    @ApiOperation(httpMethod = "GET", value = "根据店铺ID获取该店铺所售卖商品的分类")
    public Wrapper<List<ProdCategoryDto>> getShopCategoryByShopId(@ApiParam(name = "shopId", value = "shopId") @PathVariable Long shopId) {
        List<ProdCategoryDto> prodCategoryDtos = shopGoodsService.getShopCategoryIsAliveByShopId(shopId);
        return WrapMapper.ok(prodCategoryDtos);
    }

    @GetMapping("/app/shop/{shopId}/{categoryId}")
    @ApiOperation(httpMethod = "GET", value = "根据店铺ID获取该店铺所售卖商品分类下的商品")
    public Wrapper<List<GoodsInfoDto>> getShopGoodsByShopIdAndCategoryId(@ApiParam(name = "shopId", value = "shopId") @PathVariable Long shopId,
                                                                            @ApiParam(name = "categoryId", value = "categoryId") @PathVariable Long categoryId) {
        List<GoodsInfoDto> goodsInfoDtos = shopGoodsService.getShopGoodsByShopIdAndCategoryId(shopId, categoryId);
        return WrapMapper.ok(goodsInfoDtos);
    }

}
